﻿using System.Data;
using System.Security.Permissions;
using System.Text;

namespace ExcelExport
{
    public class GenerateCS
    {
        public void WriteToFile(string className, List<TableField> fieldList, DataTable dt)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(AddHead(className));

            string strField = String.Empty;

            for (int i = 0; i < fieldList.Count; i++)
            {
                TableField field = fieldList[i];
                strField += Util.NewLine;
                strField += string.Format("\t//{0}", field.explainStr);
                strField += Util.NewLine;
                strField += string.Format("\tpublic {0} {1};", field.strFieldType, field.filedName);
            }

            strField += Util.NewLine;
            strField += "}";
            sb.Append(strField);

            Utility.WriteFile(Util.dirOutCS + "/" + className + ".cs", sb.ToString());
        }

        private string AddHead(string classsName)
        {
            StringBuilder stringBuffer = new StringBuilder();
            stringBuffer.Append("public class " + classsName);
            stringBuffer.Append(Util.NewLine);
            stringBuffer.Append("{");
            return stringBuffer.ToString();
        }
    }
}
